package com.hxm.meizi.http;

import android.support.annotation.Nullable;

import com.hxm.meizi.entity.ApiClass;

import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import rx.Observable;

/**
 * Created by Hxm on 2017/2/22.
 * http 请求
 */

public class HttpRequest {
    private Retrofit retrofit = HttpManger.getInstance().getRetrofit();
    private Observable.Transformer transformer = HttpManger.getInstance().ThrowableTransformer();

    public static HttpRequest getInstance() {
        return HttpRequestHolder.instance;
    }

    /**
     * 获取html
     *
     * @param url
     * @return
     */
    @SuppressWarnings("unchecked")
    public Observable<ResponseBody> getHtml(String url) {
        ApiService service = retrofit.create(ApiService.class);
        return service.getHtml(url).compose(transformer);
    }

    /**
     * 下载图片
     *
     * @param url
     * @return
     */
    @SuppressWarnings("unchecked")
    public Observable<ResponseBody> downloadImage(String url) {
        ApiService service = retrofit.create(ApiService.class);
        return service.downloadImage(url).compose(transformer);
    }

    /**
     * 上传信息
     *
     * @return
     */
    public Observable<ResponseBody> uploadInfo(String request) {
        ApiService service = retrofit.create(ApiService.class);
        return service.uploadInfo(request);
    }

    public Observable<ArrayList<ApiClass>> getClassNames(@Nullable String classId) {
        ApiService service = retrofit.create(ApiService.class);
        return service.getClassNames(classId);
    }

    private static class HttpRequestHolder {
        private static final HttpRequest instance = new HttpRequest();
    }
}
